<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('jqgrid_search')) {
    /**
     * get the result of search object for jqgrid
     * 
     * @param object $model     new object model
     * @param string $search    jquery_grid post _search
     * @param object $filters   jquery_grid post filters after json_decode
     * @param string $order     jquery_grid post sidx
     * @param string $order_direct  jquery_grid post sord
     * @param integer $page     jquery_grid post page
     * @param integer $num_per_page jquery_grid post rows
     * @param array $relative_search array of relative of object model, which used to search
     * Ex:
     * $relative_search = array(
     *     'mode1' => array(
     *         'search' => 'id',
     *         'include' => array('id', 'code')
     *     ),
     *     'mode1||name' => array(
     *         'search' => 'name'
     *     )
     * );
     * @param string $get_type the way to get result object 'page' (default) or 'all'
     * @return obj
     */
    function jqgrid_search($model, $search, $filters, $order, $order_direct, $page, $num_per_page, $relative_search = array(), $get_type = 'page') {
    	$model->distinct();
        foreach ($relative_search as $m => $relative) {
            if (isset($relative['include']) && !empty($relative['include'])) {
                $model->include_related($m, $relative['include']);
            }
        }
        // relative fields
        $relative_key = array_keys($relative_search);
        if ($search == 'true' && !empty($filters->groupOp) && !empty($filters->rules)) {
            if (count($filters->rules) > 1)
                $model->group_start();
            if ($filters->groupOp == 'OR') {
                foreach ($filters->rules as $rules) {
                    $searchField = $rules->field;
                    $searchOper = $rules->op; // eq, ne, 'gt', 'ge', 'lt', 'le', bw, bn, ew, en, cn, nc, nu, nn, in, ni
                    $searchString = $rules->data;
                    if (in_array($searchField, $relative_key) && $searchString != '') {
                        $tmparr = explode('||', $searchField);
                        $model_related = $tmparr[0];
                        if ($searchOper == 'eq') {
                            $model->or_where_related($model_related, $relative_search[$searchField]['search'], $searchString);
                        } elseif ($searchOper == 'ne') {
                            $model->or_where_related($model_related, $relative_search[$searchField]['search'] . ' !=', $searchString);
                        } elseif ($searchOper == 'gt') {
                            $model->or_where_related($model_related, $relative_search[$searchField]['search'] . ' >', $searchString);
                        } elseif ($searchOper == 'ge') {
                            $model->or_where_related($model_related, $relative_search[$searchField]['search'] . ' >=', $searchString);
                        } elseif ($searchOper == 'lt') {
                            $model->or_where_related($model_related, $relative_search[$searchField]['search'] . ' <', $searchString);
                        } elseif ($searchOper == 'le') {
                            $model->or_where_related($model_related, $relative_search[$searchField]['search'] . ' <=', $searchString);
                        } elseif ($searchOper == 'bw') {
                            $model->or_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'after');
                        } elseif ($searchOper == 'bn') {
                            $model->or_not_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'after');
                        } elseif ($searchOper == 'ew') {
                            $model->or_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'before');
                        } elseif ($searchOper == 'en') {
                            $model->or_not_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'before');
                        } elseif ($searchOper == 'cn') {
                            $model->or_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'both');
                        } elseif ($searchOper == 'nc') {
                            $model->or_not_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'both');
                        } elseif ($searchOper == 'nu') {
                            $model->or_where_related($model_related, $relative_search[$searchField]['search'] . ' IS NULL');
                        } elseif ($searchOper == 'nn') {
                            $model->or_where_related($model_related, $relative_search[$searchField]['search'] . ' IS NOT NULL');
                        } elseif ($searchOper == 'in') {
                            $model->or_where_in_related($model_related, $relative_search[$searchField]['search'], explode(';', $searchString));
                        } elseif ($searchOper == 'ni') {
                            $model->or_where_not_in_related($model_related, $relative_search[$searchField]['search'], explode(';', $searchString));
                        }
                    } elseif (in_array($searchField, $model->fields) && $searchString != '') {
                        if ($searchOper == 'ne') {
                            $model->or_where($searchField, $searchString);
                        } elseif ($searchOper == 'ne') {
                            $model->or_where($searchField . ' !=', $searchString);
                        } elseif ($searchOper == 'gt') {
                            $model->or_where($searchField . ' >', $searchString);
                        } elseif ($searchOper == 'ge') {
                            $model->or_where($searchField . ' >=', $searchString);
                        } elseif ($searchOper == 'lt') {
                            $model->or_where($searchField . ' <', $searchString);
                        } elseif ($searchOper == 'le') {
                            $model->or_where($searchField . ' <=', $searchString);
                        } elseif ($searchOper == 'bw') {
                            $model->or_like($searchField, $searchString, 'after');
                        } elseif ($searchOper == 'bn') {
                            $model->or_not_like($searchField, $searchString, 'after');
                        } elseif ($searchOper == 'ew') {
                            $model->or_like($searchField, $searchString, 'before');
                        } elseif ($searchOper == 'en') {
                            $model->or_not_like($searchField, $searchString, 'before');
                        } elseif ($searchOper == 'cn') {
                            $model->or_like($searchField, $searchString, 'both');
                        } elseif ($searchOper == 'nc') {
                            $model->or_not_like($searchField, $searchString, 'both');
                        } elseif ($searchOper == 'nu') {
                            $model->or_where($searchField . ' IS NULL');
                        } elseif ($searchOper == 'nn') {
                            $model->or_where($searchField . ' IS NOT NULL');
                        } elseif ($searchOper == 'in') {
                            $model->or_where_in($searchField, explode(';', $searchString));
                        } elseif ($searchOper == 'ni') {
                            $model->or_where_not_in($searchField, explode(';', $searchString));
                        }
                    }
                }
            } else { // AND
                foreach ($filters->rules as $rules) {
                    $searchField = $rules->field;
                    $searchOper = $rules->op; // eq, ne, 'gt', 'ge', 'lt', 'le', bw, bn, ew, en, cn, nc, nu, nn, in, ni
                    $searchString = $rules->data;
                    if (in_array($searchField, $relative_key) && $searchString != '') {
                        $tmparr = explode('||', $searchField);
                        $model_related = $tmparr[0];
                        if ($searchOper == 'eq') {
                            $model->where_related($model_related, $relative_search[$searchField]['search'], $searchString);
                        } elseif ($searchOper == 'ne') {
                            $model->where_related($model_related, $relative_search[$searchField]['search'] . ' !=', $searchString);
                        } elseif ($searchOper == 'gt') {
                            $model->where_related($model_related, $relative_search[$searchField]['search'] . ' >', $searchString);
                        } elseif ($searchOper == 'ge') {
                            $model->where_related($model_related, $relative_search[$searchField]['search'] . ' >=', $searchString);
                        } elseif ($searchOper == 'lt') {
                            $model->where_related($model_related, $relative_search[$searchField]['search'] . ' <', $searchString);
                        } elseif ($searchOper == 'le') {
                            $model->where_related($model_related, $relative_search[$searchField]['search'] . ' <=', $searchString);
                        } elseif ($searchOper == 'bw') {
                            $model->like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'after');
                        } elseif ($searchOper == 'bn') {
                            $model->not_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'after');
                        } elseif ($searchOper == 'ew') {
                            $model->like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'before');
                        } elseif ($searchOper == 'en') {
                            $model->not_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'before');
                        } elseif ($searchOper == 'cn') {
                            $model->like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'both');
                        } elseif ($searchOper == 'nc') {
                            $model->not_like_related($model_related, $relative_search[$searchField]['search'], $searchString, 'both');
                        } elseif ($searchOper == 'nu') {
                            $model->where_related($model_related, $relative_search[$searchField]['search'] . ' IS NULL');
                        } elseif ($searchOper == 'nn') {
                            $model->where_related($model_related, $relative_search[$searchField]['search'] . ' IS NOT NULL');
                        } elseif ($searchOper == 'in') {
                            $model->where_in_related($model_related, $relative_search[$searchField]['search'], explode(';', $searchString));
                        } elseif ($searchOper == 'ni') {
                            $model->where_not_in_related($model_related, $relative_search[$searchField]['search'], explode(';', $searchString));
                        }
                    } elseif (in_array($searchField, $model->fields) && $searchString != '') {
                        if ($searchOper == 'eq') {
                            $model->where($searchField, $searchString);
                        } elseif ($searchOper == 'ne') {
                            $model->where($searchField . ' !=', $searchString);
                        } elseif ($searchOper == 'gt') {
                            $model->where($searchField . ' >', $searchString);
                        } elseif ($searchOper == 'ge') {
                            $model->where($searchField . ' >=', $searchString);
                        } elseif ($searchOper == 'lt') {
                            $model->where($searchField . ' <', $searchString);
                        } elseif ($searchOper == 'le') {
                            $model->where($searchField . ' <=', $searchString);
                        } elseif ($searchOper == 'bw') {
                            $model->like($searchField, $searchString, 'after');
                        } elseif ($searchOper == 'bn') {
                            $model->not_like($searchField, $searchString, 'after');
                        } elseif ($searchOper == 'ew') {
                            $model->like($searchField, $searchString, 'before');
                        } elseif ($searchOper == 'en') {
                            $model->not_like($searchField, $searchString, 'before');
                        } elseif ($searchOper == 'cn') {
                            $model->like($searchField, $searchString, 'both');
                        } elseif ($searchOper == 'nc') {
                            $model->not_like($searchField, $searchString, 'both');
                        } elseif ($searchOper == 'nu') {
                            $model->where($searchField . ' IS NULL');
                        } elseif ($searchOper == 'nn') {
                            $model->where($searchField . ' IS NOT NULL');
                        } elseif ($searchOper == 'in') {
                            $model->where_in($searchField, explode(';', $searchString));
                        } elseif ($searchOper == 'ni') {
                            $model->where_not_in($searchField, explode(';', $searchString));
                        }
                    }
                }
            }
            if (count($filters->rules) > 1)
                $model->group_end();
        }
        // Order
        if (!empty($order) && in_array($order_direct, array('asc', 'desc'))) {
            if (in_array($order, $relative_key)) {
                $model->order_by($order . '_' . $relative_search[$order], $order_direct);
            } elseif (in_array($order, $model->fields)) {
                $model->order_by($order, $order_direct);
            }
        }
        // Page
        if ($page <= 0)
            $page = 1;
        if ($num_per_page <= 0)
            $num_per_page = 10;

        //echo $model->get_sql(); exit;
        if ($get_type == 'page')
             return $model->get_paged($page, $num_per_page);
        else
            return $model->get_iterated();
    }

}


/* End of file jqgrid_helper.php */
/* Location: ./application/helpers/jqgrid_helper.php */